"""
@File    :   cashews.py
@Time    :   2024/02/29 11:12:35
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   cashews缓存相关工具函数
"""

from cashews import Cache

from src.config import get_app_settings
from src.enums.redis_db import RedisDbNumEnum

# 获取应用配置
settings = get_app_settings()

# 创建缓存对象
data_cache = Cache()  # 数据缓存
token_cache = Cache()  # Token缓存


class CashewsCondition:
    """cashews的缓存条件类"""

    @staticmethod
    def condition_only_true_result(result, args, kwargs, key=None) -> bool:
        """只缓存结果可表示为True的对象"""
        return bool(result)


# 配置缓存对象
data_cache.setup(settings.redis_url + str(RedisDbNumEnum.cache_db.value))
token_cache.setup(settings.redis_url + str(RedisDbNumEnum.token_db.value))

# 注册缓存的tag
# 角色菜单信息相关缓存tag
data_cache.register_tag(
    "role_menu_info", "role_menu_info:{tenant_id}:{role_id}:{need_disabled}"
)
data_cache.register_tag(
    "role_menu_info:{tenant_id}", "role_menu_info:{tenant_id}:{role_id}:{need_disabled}"
)
data_cache.register_tag(
    "role_menu_info:{tenant_id}:{role_id}",
    "role_menu_info:{tenant_id}:{role_id}:{need_disabled}",
)
